package art.client.views;

import java.util.List;

import art.client.ClientFactory;
import art.shared.entities.GenreShared;
import art.shared.entities.LoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ArtUploadView extends Composite implements IsWidget {

	private VerticalPanel mainPanel = new VerticalPanel();
	private FileUpload fileUploader = null;
	private Button uploadButton = null;
	private FormPanel form = null;
	private ListBox genreField = null;
	private TextBox titleField = null;
	private TextArea artField = null;
	private TextBox creatorField = null;

	public ArtUploadView(final ClientFactory factory, LoginInfo loginInfo,
			List<GenreShared> genres) {
		VerticalPanel panel = new VerticalPanel();
		form = new FormPanel();
		form.setAction(GWT.getHostPageBaseURL() + "artportal/artupload");
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		mainPanel.add(factory.getMenuView(loginInfo));
		titleField = new TextBox();
		titleField.setName("titleField");
		titleField.getElement().setId("artupload-titlefield");
		panel.add(new Label("Add a title:"));
		panel.add(titleField);
		genreField = new ListBox();
		genreField.setName("genreField");
		genreField.getElement().setId("artupload-genrefield");
		for (GenreShared genre : genres) {
			genreField.addItem(genre.getName());
		}
		panel.add(genreField);
		panel.add(new Label("Please upload the file containing your art (max 10 MB): "));
		fileUploader = new FileUpload();
		fileUploader.setName("file-uploader");
		panel.add(fileUploader);
		panel.add(new Label("Allowed extensions: pdf, txt, png, jpg"));
		artField = new TextArea();
		artField.setName("artField");
		artField.getElement().setId("artupload-artField");
		panel.add(artField);
		creatorField = new TextBox();
		creatorField.setEnabled(true);
		creatorField.setVisible(false);
		creatorField.setName("creatorField");
		creatorField.setText(loginInfo.getEmailAddress());
		panel.add(creatorField);
		uploadButton = new Button("Upload");
		uploadButton.getElement().setId("artupload-uploadbutton");
		panel.add(uploadButton);
		form.setWidget(panel);
		mainPanel.add(form);
		initWidget(mainPanel);
	}

	public Button getUploadButton() {
		return uploadButton;
	}

	public FormPanel getForm() {
		return form;
	}

	public ListBox getGenreField() {
		return genreField;
	}

	public TextBox getTitleField() {
		return titleField;
	}
	
	public TextArea getArtField() {
		return artField;
	}
	
	public FileUpload getFileUpload() {
		return fileUploader;
	}

	public void clearForm() {
		artField.setText("");
		titleField.setText("");
		genreField.setSelectedIndex(0);
		fileUploader = new FileUpload();
	}
}
